-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix block time parsing #479
Conversation
WalkthroughThe recent changes introduce the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- odra-casper/rpc-client/Cargo.toml (1 hunks)
- odra-casper/rpc-client/src/casper_client.rs (2 hunks)
Files skipped from review due to trivial changes (1)
- odra-casper/rpc-client/Cargo.toml
let result = result["last_added_block_info"]["timestamp"] | ||
.as_str() | ||
.unwrap_or_else(|| { | ||
panic!( | ||
"Couldn't get block time - malformed JSON response: {:?}", | ||
result | ||
) | ||
}); | ||
let timestamp: u64 = result.parse().expect("Couldn't parse block time"); | ||
timestamp | ||
let system_time = humantime::parse_rfc3339_weak(result).expect("Couldn't parse block time"); | ||
system_time | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.unwrap_or_else(|_| panic!("Couldn't parse block time")) | ||
.as_millis() as u64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor error handling in get_block_time
to improve robustness.
The current implementation uses multiple unwrap_or_else
which could be replaced with a more robust error handling strategy. Consider using a single error handling block at the end of the method to handle potential errors in a unified way. This would make the code cleaner and more maintainable.
- let result = result["last_added_block_info"]["timestamp"]
- .as_str()
- .unwrap_or_else(|| {
- panic!(
- "Couldn't get block time - malformed JSON response: {:?}",
- result
- )
- });
- let system_time = humantime::parse_rfc3339_weak(result).expect("Couldn't parse block time");
- system_time
- .duration_since(SystemTime::UNIX_EPOCH)
- .unwrap_or_else(|_| panic!("Couldn't parse block time"))
- .as_millis() as u64
+ let timestamp_str = result["last_added_block_info"]["timestamp"]
+ .as_str()
+ .ok_or_else(|| anyhow::anyhow!("Malformed JSON response: {:?}", result))?;
+ let system_time = humantime::parse_rfc3339_weak(timestamp_str)
+ .map_err(|_| anyhow::anyhow!("Couldn't parse block time"))?;
+ let block_time = system_time
+ .duration_since(SystemTime::UNIX_EPOCH)
+ .map_err(|_| anyhow::anyhow!("System time before UNIX EPOCH"))?
+ .as_millis() as u64;
+ block_time
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
let result = result["last_added_block_info"]["timestamp"] | |
.as_str() | |
.unwrap_or_else(|| { | |
panic!( | |
"Couldn't get block time - malformed JSON response: {:?}", | |
result | |
) | |
}); | |
let timestamp: u64 = result.parse().expect("Couldn't parse block time"); | |
timestamp | |
let system_time = humantime::parse_rfc3339_weak(result).expect("Couldn't parse block time"); | |
system_time | |
.duration_since(SystemTime::UNIX_EPOCH) | |
.unwrap_or_else(|_| panic!("Couldn't parse block time")) | |
.as_millis() as u64 | |
let timestamp_str = result["last_added_block_info"]["timestamp"] | |
.as_str() | |
.ok_or_else(|| anyhow::anyhow!("Malformed JSON response: {:?}", result))?; | |
let system_time = humantime::parse_rfc3339_weak(timestamp_str) | |
.map_err(|_| anyhow::anyhow!("Couldn't parse block time"))?; | |
let block_time = system_time | |
.duration_since(SystemTime::UNIX_EPOCH) | |
.map_err(|_| anyhow::anyhow!("System time before UNIX EPOCH"))? | |
.as_millis() as u64; | |
block_time |
Summary by CodeRabbit
humantime
andSystemTime
.